<template>
  <div>
    <div class="page-breadcrumb-wrap">
      <el-card class="page-breadcrumb">
        <span class="breadcrumb-button" @click="goBack">审批进展</span>
        <span class="breadcrumb-line">/</span>
        <span class="breadcrumb-active">审批进展详情</span>
      </el-card>
    </div>
    <div class="detail-box">
      <el-card>
        <el-form :style="{'height': formHeight + 'px','overflow-y': 'auto','overflow-x': 'hidden'}" ref="form" :model="dataForm" label-width="120px">
          <div class="form-header">
            <span class="form-title">审批详情</span>
            <el-button type="primary" plain @click="goBack()">返 回</el-button>
          </div>
          <el-row>
            <el-col :span="8" class="mt10">
              <el-form-item label="项目:">
                {{ dataForm.lineName }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="分部:">
                {{ dataForm.lotName }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="工区:">
                {{ dataForm.workArea }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="审批名称:">
                {{ dataForm.projectName }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="权属单位:">
                {{ dataForm.ownershipUnit }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="权属单位审批人:">
                {{ dataForm.linkName }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="联系方式:">
                {{ dataForm.linkPhone }}
              </el-form-item>
            </el-col>
          </el-row>
          <div class="form-header mt10">
            <span class="form-title">关联审批流程</span>
          </div>
          <el-row>
            <el-col :span="8" class="mt10">
              <el-form-item label="模型编码:">
                {{ dataForm.moduleCode }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="模型名称:">
                {{ dataForm.moduleName }}
              </el-form-item>
            </el-col>
            <el-col :span="8" class="mt10">
              <el-form-item label="生效版本:">
                {{ dataForm.version }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="平均审批时长:">
                {{ dataForm.averageDuration }}
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="最大审批时间:">
                {{ dataForm.maxTime }}
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="流程描述:">
                {{ dataForm.remark }}
              </el-form-item>
            </el-col>
          </el-row>
          <div class="form-header mt10" style="border-bottom:none;">
            <span class="form-title" >设计方案</span>
          </div>
          <div class="center-card">
            <div class="card-left">
              <span class="form-sub-title">涉敏区域</span>
              <div class="card-left-body">
                <el-col :span="24">
                  <el-form-item label="BIM模型:" label-width="120px">
                    {{ dataForm.sensitiveAreaBim }}
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="GIS边界文件:" label-width="120px">
                    <div style="height: 100px; overflow: auto;overflow-x: hidden;">
                      <div class="file-item" v-for="item in dataForm.gisFiles" :key="item.fileId">
                        <img src="../../../../assets/icon/antFill-file-excel.png" class="file-item-icon" alt="">
                        <div class="file-item-content">
                          <div class="file-item-text" :title="item.fileName">{{ item.fileName }}</div>
                          <div class="file-item-button">
                            <el-button type="text" @click="download(item)">下载</el-button>
                            <el-button type="text" @click="filePreview(item)">预览</el-button>
                          </div>
                        </div>
                      </div>
                    </div>
                  </el-form-item>
                </el-col>
              </div>
            </div>
            <div class="card-line"></div>
            <div class="card-center">
              <span class="form-sub-title">施工设计</span>
              <div class="card-center-body">
                <el-col :span="24">
                  <el-form-item label="BIM模型:" label-width="120px">
                    {{ dataForm.constructDesignBim }}
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="施工方案文件:" label-width="120px">
                    <div style="height: 100px; overflow: auto;overflow-x: hidden;">
                      <div class="file-item" v-for="item in dataForm.schemeFiles" :key="item.fileId">
                        <img src="../../../../assets/icon/antFill-file-excel.png" class="file-item-icon" alt="">
                        <div class="file-item-content">
                          <div class="file-item-text" :title="item.fileName">{{ item.fileName }}</div>
                          <div class="file-item-button">
                            <el-button type="text" @click="download(item)">下载</el-button>
                            <el-button type="text" @click="filePreview(item)">预览</el-button>
                          </div>
                        </div>
                      </div>
                    </div>
                  </el-form-item>
                </el-col>
              </div>
            </div>
          </div>
          <div class="form-header" style="margin-top: 40px">
            <span class="form-title">审批进度</span>
            <el-button type="text" @click="viewFlowPicture">查看流程图</el-button>
          </div>
          <div style="padding: 10px">
            <el-steps :active="active" align-center>
              <el-step
                v-for="(item, index) in stepList"
                :title="item.taskName + ' ' + item.userName"
                :key="index"
                :icon="!item.endTime ? 'el-icon-info' : 'el-icon-circle-check'"
              >
                <template #description>
                  <div>
                    <div style="color: #303133;">{{ item.endTime }}</div>
                    <el-popover placement="top" trigger="click">
                      <div class="file-item" v-for="ele in item.attachments" :key="ele.fileId">
                        <img src="../../../../assets/icon/antFill-file-excel.png" class="file-item-icon" alt="">
                        <div class="file-item-content" :title="ele.originalFileName">
                          <div class="file-item-text">{{ ele.originalFileName }}</div>
                          <div class="file-item-button">
                            <el-button type="text" @click="download(ele)">下载</el-button>
                            <el-button type="text" @click="filePreview(ele)">预览</el-button>
                          </div>
                        </div>
                      </div>
                      <el-button slot="reference" v-if="item.attachments.length > 0" type="text">查看</el-button>
                    </el-popover>
                  </div>
                  <div :title="item.comment" style="color: red;width:100%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">{{ item.comment }}</div>
                </template>
              </el-step>
            </el-steps>
          </div>
        </el-form>
      </el-card>
    </div>
    <flow-picture ref="flowPictureRef" />
  </div>
</template>
<script>
import { getDetail, getModuleById, download } from '@/api/constructionScheme'
import { getProcessHistory } from '@/api/constructionSchemeApprovalProgress'
import FlowPicture from '@/views/designingScheme/designingScheme/components/FlowPicture.vue'

export default {
  name: 'DetailPage',
  components: {
    FlowPicture
  },
  data () {
    return {
      dataForm: {},
      active: 1,
      stepList: [],
      formHeight: 500
    }
  },
  mounted () {
    // 监听窗口大小变化事件
    window.addEventListener('resize', this.setTableHeight)
    // 初始化表格高度
    this.setTableHeight()
  },
  methods: {
    filePreview (item) {
      this.$filePreview(item)
    },
    setTableHeight () {
      this.$nextTick(() => {
        this.formHeight = document.body.offsetHeight - 160
      })
    },
    async init (row) {
      console.log(row)
      const res = await getDetail({ schemeId: row.id })
      console.log(res, '详情')
      this.dataForm = res.data
      const result = await getModuleById({ modelId: this.dataForm.moduleId, versionId: res.data.versionId })
      this.dataForm.moduleName = result.data.moduleName
      this.dataForm.version = result.data.version
      this.dataForm.moduleCode = result.data.moduleCode
      this.dataForm.remark = result.data.remark
      this.$forceUpdate()
      const result1 = await getProcessHistory({ processId: res.data.processInstanceId })
      this.stepList = result1.data
      console.log(this.stepList, '审批进度')
      const timeData = []
      let timeTotal = 0
      let timeNum = 0
      this.stepList.forEach((item, index) => {
        if (item.durationInMillis && item.durationInMillis > 0) {
          timeNum++
          timeData.push(item.durationInMillis)
          timeTotal += item.durationInMillis
        }
        if (item.endTime) {
          this.active = index + 1
        }
        // if (this.stepList[this.stepList.length - 1].endTime) {
        //     this.active = this.stepList.length
        //   }
      })
      if (timeData.length > 0) {
        this.dataForm.maxTime = this.convertMilliseconds(Math.max(...timeData))
      }
      if (timeTotal > 0 && timeNum > 0) {
        this.dataForm.averageDuration = this.convertMilliseconds((timeTotal / timeNum))
      }
    },
    convertMilliseconds (millis) {
      const hours = Math.floor(millis / (1000 * 60 * 60))
      const minutes = Math.floor((millis % (1000 * 60 * 60)) / (1000 * 60))
      const seconds = Math.floor((millis % (1000 * 60)) / 1000)
      return `${hours}时${minutes}分${seconds}秒`
    },
    goBack () {
      this.$emit('changeShowType', 1)
    },
    // 下载文件
    download (row) {
      download(row.fileId).then((res) => {
        console.log(res)
        const blob = new Blob([res.res])
        const url = window.URL.createObjectURL(blob)
        const link = document.createElement('a') // 创建a标签
        link.href = url
        link.download = row.originalFileName || row.fileName // 重命名文件
        link.click()
        URL.revokeObjectURL(url) // 释放内
      })
    },
    // 查看流程图
    viewFlowPicture () {
      this.$nextTick(() => {
        this.$refs.flowPictureRef.init(this.dataForm.processInstanceId)
      })
    }
  }
}
</script>
<style scoped lang="scss">
.mt10 {
  margin-top: 10px;
}

.detail-box {
  padding: 10px;
  padding-top: 0;
}

.form-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  border-bottom: 1px solid #ddd;
}

.form-title {
  color: #101010;
  font-size: 16px;
  font-weight: bold;
}

.form-sub-title {
  margin-bottom: 10px;
  color: #000;
  font-size: 15px;
  font-weight: bold;
}

.center-card {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 20px;
  border-radius: 4px;
  border: 1px solid #ebeef5;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-left {
  width: calc(49% - 21px);

  .card-left-body {
    padding: 10px 30px;
    height: 200px;
  }
}

.card-center {
  width: calc(49% - 21px);

  .card-center-body {
    padding: 10px 30px;
    height: 200px;
  }
}

.card-line {
  margin: 0 20px;
  height: 180px;
  width: 1px;
  background: #ddd;
}
.file-item {
  display: flex;
  margin-bottom: 10px;
}

.file-item-icon {
  width: 45px;
  height: 50px;
}

.file-item-content {
  margin-left: 10px;
  height: 50px;
   width: calc(100% - 45px);
  .file-item-text {
    height: 25px;
    font-weight: 700;
    width: 100%;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .file-item-button {
    height: 25px;
  }
}
.card-right {
  width: calc(33% - 21px);

  .card-right-body {
    padding: 10px 30px;
    // height: 120px;
  }
}

::v-deep .el-step__title.is-finish {
  font-weight: 700;
  color: #303133;
}
::v-deep .el-step__title.is-wait {
  font-weight: 700;
  color: #303133;
}
::v-deep .el-step__head.is-success {
  color: #409eff;
  border-color: #409eff;
}
::v-deep .el-icon-info {
  color: #eca600;
}

::v-deep .el-step__title {
  font-size: 14px;
}
::v-deep .el-step {
  margin-top: 20px;
  flex-basis: 20% !important;
  max-width: none !important;
}
::v-deep .el-steps {
  flex-wrap: wrap;
}
.backBox {
  margin: 20px 0;
  text-align: center;
}
</style>
